package edu.moravian.game;

import edu.moravian.UI.game.BouncingBallGameView;
import edu.moravian.entity.BallEntityManager;
import edu.moravian.math.Point2D;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.event.KeyEvent;


/**
 * The bouncing ball game simulates a bunch of balls
 * moving around the world colliding with each other and the 
 * bounds of the world
 * 
 * @author stejr02
 */
public class BouncingBallGame implements Game
{
    private boolean done;

    private BouncingBallGameView gameView;
    private BallEntityManager ballManager;
    
    /**
     * Create the game
     *
     * @param worldWidth the width of the world
     * @param worldHeight the height of the world
     */
    public BouncingBallGame(WorldDescriptor world) 
    {
        ballManager = new BallEntityManager(world);
        gameView = new BouncingBallGameView(world, ballManager);
        
        done = false;
    }

   
    /**
     * Update the game to the next frame
     */
    @Override
    public void update(double delta)
    {      
        ballManager.update(delta); 
    }
       

    
    /**
     * Draw the current game state
     * 
     * @param w2d the graphics to draw to
     */
    @Override
    public void draw(World2D w2d)
    {
        gameView.draw(w2d);
    }

    /**
     * Determine whether the game is done. 
     * 
     * @return whether the player wanted the game to end
     */
    @Override
    public boolean done()
    {
        return done;
    }

    
    
        
    /**
     * Handle for key presses to control the players motion. 
     * 
     * @param e the key event
     */
    @Override
    public void handleKeyPress(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_DOWN)
        {
            ballManager.decreaseElasticity();
        }
        else if(e.getKeyCode() == KeyEvent.VK_UP)
        {
            ballManager.increaseElasticity();
        }
    }


    /**
     * Handle for key release to control the players motion.
     * 
     * @param e the key event
     */
    @Override
    public void handleKeyRelease(KeyEvent e)
    {
      
    }
 

    /**
     * A handler method for key events 
     * 
     * @param e 
     */
    @Override
    public void handleKeyTyped(KeyEvent e) 
    {
        //'q' is the character to quit at anypoint in the game
        if(e.getKeyChar() == 'q')
        {
            done = true;
        }
        
        else if(e.getKeyChar() == 'e' ||  e.getKeyChar() == 'E')
        {
            gameView.changeDisplayEpsilon();
        }
    }


    /**
     * Mouse movement handler
     * 
     * @param e the mouse event
     */
    @Override
    public void handleMouseMoved(Point2D mouseLocation) 
    {
        //In this game, do nothing
    }

    
}
